package cn.xuchunh.guava.base;

import com.google.common.base.Throwables;

/**
 * Created by XCH on 2017/3/30.
 *
 * <p>
 * <a href="http://wiki.jikexueyuan.com/project/google-guava-official-tutorial/throwables.html">Throwables：简化异常和错误的传播与检查</a>
 *
 * <table>
 *     <tr>
 *         <td>RuntimeException propagate(Throwable)</td>
 *         <td>如果 Throwable 是 Error 或 RuntimeException，直接抛出；否则把 Throwable 包装成 RuntimeException 抛出。返回类型是 RuntimeException，所以你可以像上面说的那样写成throw Throwables.propagate(t)，Java 编译器会意识到这行代码保证抛出异常。</td>
 *     </tr>
 *     <tr>
 *         <td>void propagateIfInstanceOf( Throwable, Class<X extends Exception>) throws X</td>
 *         <td>Throwable 类型为 X 才抛出</td>
 *     </tr>
 *     <tr>
 *         <td>void propagateIfPossible( Throwable)</td>
 *         <td>Throwable 类型为 Error 或 RuntimeException 才抛出</td>
 *     </tr>
 *     <tr>
 *         <td>void propagateIfPossible( Throwable, Class<X extends Throwable>) throws X</td>
 *         <td>Throwable 类型为 X, Error 或 RuntimeException 才抛出</td>
 *     </tr>
 * </table>
 *
 * @author XCH
 * @version 1.0.0-SNAPSHOT
 */
public class DemonThrowable {

    public static void main(String[] args) {
        String s = null;
        try {
            s.length();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(Throwables.getRootCause(e));
            System.out.println(Throwables.getCausalChain(e));
            System.out.println(Throwables.getStackTraceAsString(e));
        }
    }

}
